﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace PayloadTracker.APRS
{
    /// <summary>
    /// Class representing one APRS beacon received. 
    /// </summary>
    [Serializable]
    public class APRSBeacon
    {
        string m_Callsign = string.Empty;
        Coordinate m_Position = new Coordinate();
        float m_Altitude = float.MinValue;
        float m_Course = float.MinValue;
        float m_Speed = float.MinValue;
        DateTime m_Time = DateTime.MinValue;
        object[] m_ImageIndex = new object[2] { "/",0};


        #region Accessors
        [XmlAttribute]
        /// <summary>
        /// Callsign
        /// </summary>
        public string Callsign
        {
            get { return m_Callsign; }
            set { m_Callsign = value; }
        }
        /// <summary>
        /// APRS Symbol
        /// </summary>
        [XmlElement("ImageIndex")]
        public object[] ImageIndex
        {
            get { return m_ImageIndex; }
            set { m_ImageIndex = value; }
        }
        /// <summary>
        /// Position
        /// </summary>
        public Coordinate Position
        {
            get { return m_Position; }
            set { m_Position = value; }
        }
        /// <summary>
        /// Altitude in feet.
        /// </summary>
        public float Altitude
        {
            get { return m_Altitude; }
            set { m_Altitude = value; }
        }
        /// <summary>
        /// Bearing of payload
        /// </summary>
        public float Course
        {
            get { return m_Course; }
            set { m_Course = value; }
        }
        /// <summary>
        /// Time when payload was received. In local time.
        /// </summary>
        public System.DateTime Time
        {
            get { return m_Time; }
            set { m_Time = value; }
        }
        /// <summary>
        /// The speed in MPH.
        /// </summary>
        public float Speed
        {
            get { return m_Speed; }
            set { m_Speed = value; }
        }
#endregion

        #region SerializationProperties
        public bool ShouldSerializeTime()
        {
            return m_Time != DateTime.MinValue;
        }
        public bool ShouldSerializeCourse()
        {
            return m_Course != float.MinValue;
        }
        public bool ShouldSerializeAltitude()
        {
            return m_Altitude != float.MinValue;
        }
        public bool ShouldSerializePosition()
        {
            return m_Position != Coordinate.EMPTY;
        }
        public bool ShouldSerializeSpeed()
        {
            return m_Speed != float.MinValue;
        }
        #endregion


        #region Constructors

        public APRSBeacon(string callsign, Coordinate position, DateTime time, float altitude, float course, float speed, object[] imageIndex)
        {
            m_Callsign = callsign;
            m_Position = position;
            m_Time = time;
            m_Altitude = altitude;
            m_Course = course;
            m_Speed = speed;
            m_ImageIndex = imageIndex;
        }

        public APRSBeacon(string callsign, Coordinate position, DateTime time, float altitude, float course, float speed)
        {
            m_Callsign = callsign;
            m_Position = position;
            m_Time = time;
            m_Altitude = altitude;
            m_Course = course;
            m_Speed = speed;
        }
        
        public APRSBeacon(string callsign, Coordinate position, DateTime time, float altitude, float course)
        {
            m_Callsign = callsign;
            m_Position = position;
            m_Time = time;
            m_Altitude = altitude;
            m_Course = course;
        }
        public APRSBeacon(string callsign, Coordinate position, DateTime time, float altitude)
        {
            m_Callsign = callsign;
            m_Position = position;
            m_Time = time;
            m_Altitude = altitude;
        }
        public APRSBeacon(string callsign, Coordinate position, DateTime time)
        {
            m_Callsign = callsign;
            m_Position = position;
            m_Time = time;
        }
        public APRSBeacon(string callsign, Coordinate position)
        {
            m_Callsign = callsign;
            m_Position = position;
        }
        public APRSBeacon(string callsign)
        {
            m_Callsign = callsign;
        }
        public APRSBeacon()
        { }
        #endregion
        #region Overrides
        public override string ToString()
        {
            string retstr = string.Empty;
            if (Callsign != string.Empty)
                retstr += Callsign;
            //if (ImageIndex[0].ToString() != null && ImageIndex[1].ToString() != null)
                //retstr += " " + ImageIndex[0].ToString() + ImageIndex[1].ToString();
            if (Position != Coordinate.EMPTY)
                retstr += " " + Position;
            if (Time != DateTime.MinValue)
                retstr += " " + Time.ToShortTimeString();
            if (Altitude != float.MinValue)
                retstr += " A:" + Altitude.ToString();
            if (Course != float.MinValue)
                retstr += " C:" + Course.ToString();
            if (Speed != float.MinValue)
                retstr += " S:" + Speed.ToString();



            return retstr;
        }
        #endregion

    }
}
